﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;
namespace DAO
{
    public class DatVeChuyenBayDAO
    {
        public int Datve(DatVeChuyenBayDTO Datve)
        {
            String sql = string.Format("insert into PhieuDatCho(MaChuyenBay,MaHanhKhach,MaHangVe,GiaTien,NgayDat) values ('{0}','{1}','{2}','{3}','{4}')", Datve.MacB, Datve.MahK, Datve.MahangVe,Datve.GiaTien, Datve.NgayDat);
            SqlConnection conn = DataProvider.ConnectDB("QLVCB.mdf");
            SqlCommand cmd = new SqlCommand(sql, conn);
            int res = -1;
            res = cmd.ExecuteNonQuery();
            return res;
        }
        public List<DatVeChuyenBayDTO> loadvecb()
        {


            String sql = "SELECT pdc.MaChuyenBay,hv.TenHangVe,hk.TenHanhKhach,pdc.MaHanhKhach from PhieuDatCho pdc,HangVe hv,HanhKhach hk where pdc.MaHangVe=hv.MaHangVe and hk.MaHanhKhach=pdc.MaHanhKhach";

            SqlConnection conn = DataProvider.ConnectDB("QLVCB.mdf");
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            List<DatVeChuyenBayDTO> ds = new List<DatVeChuyenBayDTO>();

            if (reader != null)
            {
                while (reader.Read())
                {

                    DatVeChuyenBayDTO datve = new DatVeChuyenBayDTO();
                    HangVeDTO hve = new HangVeDTO();
                    HanhKhachDTO hk = new HanhKhachDTO();
                    datve.MacB = reader.GetString(0);
                    datve.MahK = reader.GetString(1);
                    datve.HangVe = hve;
                    hve.TenHangVe = reader.GetString(1);
                    datve.HanhKhach = hk;
                    hk.TenHK = reader.GetString(2);
                    ds.Add(datve);
                }
            }
            conn.Close();
            return ds;

        }
    }
}
